from django.http import JsonResponse
from django.shortcuts import render

# Create your views here.
from rest_framework.response import Response

from System import models
from rest_framework.views import APIView
from System.serializers import NavSerializer,CarouselSerializer
#查询nav，返回json数据
#继承rest_framework APIView
class Nav(APIView):
    # 使用rest_framework用户认证，authentication_classes = []表示无需登录
    authentication_classes = []
    def get(self, request, *args, **kwargs):
        #查询nav表，返回了QuerySet对象
        res = models.Nav.objects.all().values('id','name')
        #调用序列化方法，把对象转化为字符串 序列化就是把对象转换成字符串，反序列化就是把字符串转换成对象
        serializer = NavSerializer(res, many=True)
        result = {'status': 200, 'msg': '获取成功','data':serializer.data}
        #Response返回字符串对象，返回对象报错
        return Response(result)

class Carousel(APIView):
    # 使用rest_framework用户认证，authentication_classes = []表示无需登录
    authentication_classes = []
    def get(self, request, *args, **kwargs):
        #查询nav表，返回了QuerySet对象
        res = models.Carousel.objects.filter(status=1).values('id','name','img').order_by('ctime')[:5]
        #调用序列化方法，把对象转化为字符串 序列化就是把对象转换成字符串，反序列化就是把字符串转换成对象
        serializer = CarouselSerializer(res, many=True)
        result = {'status': 200, 'msg': '获取成功','data':serializer.data}
        #Response返回字符串对象，返回对象报错
        return Response(result)